package com.note.back.service;

import com.note.back.mapper.NoteMapper;
import com.note.back.pojo.Note;
import com.note.back.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class NoteService {
    @Autowired
    private NoteMapper noteMapper;

    public void add(Note note) {
        //补充属性值
        note.setCreateTime(LocalDateTime.now());
        note.setUpdateTime(LocalDateTime.now());
        Subject subject = SecurityUtils.getSubject();
        User u = (User)subject.getPrincipal();
        note.setAuthor(u.getId());
        noteMapper.add(note);
    }

    public List<Note> list() {
        Subject subject = SecurityUtils.getSubject();
        User u = (User)subject.getPrincipal();
        return noteMapper.list(u.getId());
    }

    public void update(Note note) {
        note.setUpdateTime(LocalDateTime.now());
        noteMapper.update(note);
    }

    public Note findById(Integer id) {
        return noteMapper.findById(id);
    }

    public void delete(Integer id) {
        noteMapper.delete(id);
    }

    public List<Note> searchTitle(String title) {
        Subject subject = SecurityUtils.getSubject();
        User u = (User)subject.getPrincipal();
        return noteMapper.searchTitle(title,u.getId());
    }

    public List<Note> searchCategory(Integer categoryId) {
        Subject subject = SecurityUtils.getSubject();
        User u = (User)subject.getPrincipal();
        return noteMapper.searchCategory(categoryId,u.getId());
    }

    public List<Note> searchCreateTime(String startTime, String endTime) {
        Subject subject = SecurityUtils.getSubject();
        User u = (User)subject.getPrincipal();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);
        System.out.println(start+" and "+end);
        return noteMapper.searchCreateTime(start,end,u.getId());
    }

    public List<Note> searchUpdateTime(String startTime, String endTime) {
        Subject subject = SecurityUtils.getSubject();
        User u = (User)subject.getPrincipal();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);
        return noteMapper.searchUpdateTime(start,end,u.getId());
    }

    public void save(Note note) {
        //补充属性值
        note.setUpdateTime(LocalDateTime.now());
        noteMapper.save(note);
    }
//    @Autowired
//    NoteDao noteDao;
//    @Autowired
//    CategoryDao categoryDao;
//
//    public List<Note> getAll(){
//        return noteDao.findAll(Sort.by(Sort.Direction.DESC,"id"));
//    }
//
//    public List<Note> getNotesByCategory(int id){
//        Category category = categoryDao.getOne(id);
//        return noteDao.findAllByCategory(category);
//    }
//
//    public Note getById(int id){
//        return noteDao.findById(id).get();
//    }
//
//    public void updateNote(Note note){
//        noteDao.save(note);
//    }
//
//    public void deleteById(int id) {
//        noteDao.deleteById(id);
//    }

}
